package com.xuexuehai.carrecommand.job;

import com.xuexuehai.carrecommand.dao.IUserDao;
import com.xuexuehai.carrecommand.entity.User;
import com.xuexuehai.carrecommand.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.quartz.QuartzJobBean;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author xuexuehai
 * @mailBox xuehai.xue@QQ.com
 * @date 2021/8/19 10:12 上午
 */
@Slf4j
public class MyJob extends QuartzJobBean {

    @Autowired
    private IUserDao iUserDao;
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        //定时器内容
        if(redisTemplate.opsForValue().size("usernames") != 0){
            redisTemplate.delete("usernames");
        }
        List<String> usernamesList = new ArrayList<>();
        List<User> userList = iUserDao.findAll();
        for (User user: userList) {
            usernamesList.add(user.getUsername());
        }
        redisTemplate.opsForValue().set("usernames",usernamesList);


        log.debug("---> 从数据库读取用户名 ，当前时间" + new Date());
        log.debug("---> 当前读取存放到redis缓存中的名称列表为：" + redisTemplate.opsForValue().get("usernames"));

    }
}
